﻿#define _CRT_SECURE_NO_WARNINGS 1
//寻找数组的中⼼下标（easy）: https://leetcode.cn/problems/find-pivot-index/description/
class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        int n = nums.size();
        vector<long long> dp_pre(n);
        vector<long long> dp_aft(n);

        for (int i = 1; i < n; i++)  //前缀和
        {
            dp_pre[i] = dp_pre[i - 1] + nums[i - 1];
        }
        for (int i = n - 2; i >= 0; i--)  //后缀和
        {
            dp_aft[i] = dp_aft[i + 1] + nums[i + 1];
        }

        for (int i = 0; i < n; i++)
        {
            if (dp_aft[i] == dp_pre[i])
            {
                return i;
            }
        }

        return -1;
    }
};

//除⾃⾝以外数组的乘积（medium）: https://leetcode.cn/problems/product-of-array-except-self/
class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp_pre(n, 1);
        vector<int> dp_aft(n, 1);
        vector<int> mul;

        for (int i = 1; i < n; i++)  //前缀和
        {
            dp_pre[i] = dp_pre[i - 1] * nums[i - 1];
        }
        for (int i = n - 2; i >= 0; i--)  //后缀和
        {
            dp_aft[i] = dp_aft[i + 1] * nums[i + 1];
        }

        for (int i = 0; i < n; i++)
        {
            mul.push_back(dp_aft[i] * dp_pre[i]);
        }

        return mul;
    }
};